package com.zhangxu.basemvp.model;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.orhanobut.logger.Logger;
import com.zhangxu.basemvp.bean.UserBean;
import com.zhangxu.basemvp.api.LoginApi;
import com.zhangxu.basemvp.base.BaseResponse;
import com.zhangxu.basemvp.contract.LoginContract;
import com.zhangxu.basemvp.utils.RetrofitManager;

import java.util.HashMap;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Time:2019/4/10
 * Author:zhangxu
 */
public class LoginModelImpl implements LoginContract.Model {
    @Override
    public void login(HashMap map, final LoginContract.Presenter loginPresenter) {
//        RetrofitManager.getInstance().createReq(LoginApi.class)
//                .login(map).enqueue(new Callback<BaseResponse<UserBean>>() {
//            @Override
//            public void onResponse(Call<BaseResponse<UserBean>> call, Response<BaseResponse<UserBean>> response) {
//                if (response.isSuccessful()) {
//                    BaseResponse<UserBean> user = response.body();
//                    if (user.isSuccess()) {
//                        loginPresenter.onSuccess(user.getData());
//                    } else {
//                        loginPresenter.onfail("获取用户信息失败");
//                    }
//
//                }
//
//            }
//
//            @Override
//            public void onFailure(Call<BaseResponse<UserBean>> call, Throwable t) {
//                loginPresenter.onfail(t.getMessage());
//            }
//        });
        RetrofitManager.getInstance().createReq(LoginApi.class)
                .toLogin(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseResponse<UserBean>>() {
                    @Override
                    public void onSubscribe(Disposable d) {


                    }

                    @Override
                    public void onNext(BaseResponse<UserBean> bean) {
                        if (bean.isSuccess()) {
                            loginPresenter.onSuccess(bean.getData());
                        } else {
                            loginPresenter.onfail("获取用户信息失败");
                        }


                    }


                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });


    }
}
